﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using Benefit.Models.Sys;
namespace Benefit.Models.Year
{
    public class BatchAvgYear
    {
        [Key]
        public int Id { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public int BatchId { get; set; }
        /// <summary>
        /// 操作账户
        /// </summary>
        public virtual Batch Batch { get; set; }

        public int Year { get; set; }

        /// <summary>
        /// 平仓盈亏
        /// </summary>
        public double Profit { get; set; }
        /// <summary>
        /// 手续费
        /// </summary>
        public double Free { get; set; }
        /// <summary>
        /// 净收益
        /// </summary>
        public double DayCount { get; set; }
        /// <summary>
        /// 交易笔数
        /// </summary>
        public double BillCount { get; set; }
        /// <summary>
        /// 总亏损
        /// </summary>
        public double SumLost { get; set; }
        /// <summary>
        /// 总盈利
        /// </summary>
        public double SumWin { get; set; }
        /// <summary>
        /// 保证金占用
        /// </summary>
        public double UsedMargin { get; set; }

        /// <summary>
        /// 资金利用率
        /// </summary>
        public double Utilization { get; set; }
        /// <summary>
        /// 胜率
        /// </summary>
        public double WinRate { get; set; }
        /// <summary>
        /// 涨幅
        /// </summary>
        public double RaiseRate { get; set; }

        /// <summary>
        /// 每笔盈利
        /// </summary>
        public double AvgBillBenefit { get; set; }

        /// <summary>
        /// 超过止损次数
        /// </summary>
        public double OverStopLoss { get; set; }

        /// <summary>
        /// 风险次数
        /// </summary>
        public int RiskCount { get; set; }

        /// <summary>
        /// 收益率
        /// </summary>
        public double Yield { get; set; }
        /// <summary>
        /// 最大回撤
        /// </summary>
        public double MaxReturn { get; set; }
        /// <summary>
        /// 平均持仓时间
        /// </summary>
        public double AvgPostionTime { get; set; }
        /// <summary>
        /// 平进平出单量
        /// </summary>
        public int PingJinPing { get; set; }
        /// <summary>
        /// 盈利次数
        /// </summary>
        public int WinCount { get; set; }
        /// <summary>
        /// 亏损次数
        /// </summary>
        public int LoseCount { get; set; }


        ///// <summary>
        ///// 删除当周所有记录
        ///// </summary>
        ///// <param name="weekId"></param>
        //public void Delete(int year, DB.DBManager db)
        //{
        //    var query = from t in db.BatchAvgYear where t.Year == year select t;
        //    foreach (Data.Year.BatchAvgYear c in query)
        //    {
        //        db.BatchAvgYear.Remove(c);
        //    }
        //    db.SaveChanges();
        //}

        ///// <summary>
        ///// 保存月同批次交易员均值
        ///// </summary>
        ///// <param name="tradeHistoryId">插入时间编号</param>
        ///// <param name="pdate">插入时间</param>
        //public void InitBatchAvgYear(int year, DB.DBManager db)
        //{
        //    List<Sys.Batch> batch = db.Batches.ToList();
        //    foreach (Sys.Batch b in batch)
        //    {
        //        BatchAvgYear w = new BatchAvgYear();
        //        w.BatchId = b.Id;
        //        w.Year = year;

        //        var queryExist = from t in db.BatchAvgYear where t.BatchId == b.Id && t.Year == year select t;
        //        if (queryExist.Count() > 0)
        //        {
        //            w = queryExist.First();
        //        }


        //        var query = from t in db.BatchAvgDay where t.TradeHistory.YPdate == year && t.BatchId == b.Id select t;
        //        if (query.Count() > 0)
        //        {
        //            w.BillCount = query.Average(a => a.BillCount);
        //            w.DayCount = query.Average(a => a.DayCount);
        //            w.Free = query.Average(a => a.Free);
        //            w.LoseCount = Convert.ToInt32(query.Average(a => a.LoseCount));
        //            w.Profit = query.Average(a => a.Profit);
        //            w.SumLost = query.Average(a => a.SumLost);
        //            w.SumWin = query.Average(a => a.SumWin);
        //            w.UsedMargin = query.Average(a => a.UsedMargin);
        //            w.WinCount = Convert.ToInt32(query.Average(a => a.WinCount));
        //            w.AvgBillBenefit = query.Average(a => a.AvgBillBenefit);
        //            w.AvgPostionTime = query.Average(a => a.AvgPostionTime);
        //            w.MaxReturn = query.Average(a => a.MaxReturn);
        //            w.OverStopLoss = query.Average(a => a.OverStopLoss);
        //            w.PingJinPing = Convert.ToInt32(query.Average(a => a.PingJinPing));
        //            w.RaiseRate = query.Average(a => a.RaiseRate);
        //            w.RiskCount = Convert.ToInt32(query.Average(a => a.RiskCount));
        //            w.Utilization = query.Average(a => a.Utilization);
        //            w.WinRate = query.Average(a => a.WinRate);
        //            w.Yield = query.Average(a => a.Yield);
        //            w.Year = query.First().TradeHistory.YPdate;
        //            if (queryExist.Count() == 0)
        //            {
        //                db.BatchAvgYear.Add(w);
        //            }
        //        }
        //    }
        //    db.SaveChanges();
        //}
        ///// <summary>
        ///// 获取同批次交易员年均值
        ///// </summary>
        ///// <param name="batchid"></param>
        ///// <param name="year"></param>
        ///// <param name="db"></param>
        ///// <returns></returns>
        //public BatchAvgYear GetBatchAvgYear(int batchid, int year, DB.DBManager db)
        //{
        //    var query = db.BatchAvgYear.Where(a => a.BatchId == batchid).Where(a => a.Year == year);
        //    if (query.Count() > 0)
        //    {
        //        return query.First();
        //    }
        //    else
        //    {
        //        return new Data.Year.BatchAvgYear();
        //    }
        //}


    }
}
